package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.PageExtra;
import com.example.demo.dto.RoleConfigDTO;
import com.example.demo.request.CreateRoleConfigRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.RoleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role-configs")

public class RoleConfigController {

    @Autowired
    private RoleConfigService roleConfigService;


    @GetMapping
    public ResponseEntity<ApiResponse<List<RoleConfigDTO>>> getRoleConfigs(
             @RequestParam(defaultValue = "0") int page,
             @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {

        List<RoleConfigDTO> roleConfigs = roleConfigService.getRoleConfigs(page, size);
        long total = roleConfigService.countRoleConfigs();
        PageExtra extra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(roleConfigs, "请求成功", request.getRequestURI(), extra)
        );
    }


    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<RoleConfigDTO>> getRoleConfig(
             @PathVariable Integer id,
            HttpServletRequest request) {

        RoleConfigDTO roleConfig = roleConfigService.getRoleConfigById(id);
        return ResponseEntity.ok(
                ApiResponse.success(roleConfig, "查询成功", request.getRequestURI(), null)
        );
    }


    @PostMapping
    public ResponseEntity<ApiResponse<RoleConfigDTO>> createRoleConfig(
            @Valid @RequestBody CreateRoleConfigRequest request,
            HttpServletRequest requestObj) {

        com.example.demo.entity.RoleConfig roleConfig = roleConfigService.createRoleConfig(request);
        RoleConfigDTO dto = RoleConfigDTO.fromEntity(roleConfig);
        return ResponseEntity.status(201).body(
                ApiResponse.created(dto, "角色配置创建成功", requestObj.getRequestURI())
        );
    }


    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<RoleConfigDTO>> updateRoleConfig(
            @PathVariable Integer id,
            @Valid @RequestBody CreateRoleConfigRequest request,
            HttpServletRequest requestObj) {

        roleConfigService.updateRoleConfig(id, request);
        RoleConfigDTO roleConfig = roleConfigService.getRoleConfigById(id);
        return ResponseEntity.ok(
                ApiResponse.success(roleConfig, "角色配置更新成功", requestObj.getRequestURI(), null)
        );
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteRoleConfig(
            @PathVariable Integer id,
            HttpServletRequest requestObj) {

        roleConfigService.deleteRoleConfig(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "角色配置删除成功", requestObj.getRequestURI(), null)
        );
    }
    
    @GetMapping("/search")

    public ResponseEntity<ApiResponse<List<RoleConfigDTO>>> searchRoleConfigsByRoleName(@RequestParam String roleName, HttpServletRequest request) {
        List<RoleConfigDTO> roleConfigs = roleConfigService.searchRoleConfigsByRoleName(roleName);
        return ResponseEntity.ok(
                ApiResponse.success(roleConfigs, "查询成功", request.getRequestURI(), null)
        );
    }
}